package com.ocean.service.news.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocean.dao.news.AuthorDao;
import com.ocean.dto.AuthorDto;
import com.ocean.dto.AuthorQueryDto;
import com.ocean.entity.news.Author;
import com.ocean.entity.user.User;
import com.ocean.service.news.AuthorService;
import com.ocean.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AuthorServiceImpl implements AuthorService {
    @Autowired
    private AuthorDao dao;

    @Override
    public R save(Author obj) {
        if(dao.insert(obj)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public R delete(int id) {
        if(dao.delete(id)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public R update(Author obj) {
        if(dao.update(obj)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public R queryPage(int page, int size) {
        PageHelper.startPage(page, size);
        PageInfo<Author> pageInfo=new PageInfo<>(dao.selectAll());
        return R.ok(pageInfo);
    }

    @Override
    public R queryPageWhere(AuthorQueryDto dto) {
        PageHelper.startPage(dto.getPage(), dto.getSize());
        PageInfo<Author> pageInfo=new PageInfo<>(dao.selectWhere(dto.getName(),dto.getJob()));
        return R.ok(pageInfo);
    }

    @Override
    public R importData(MultipartFile file) {
        //1.验证上传的内容是否非空
        if(!file.isEmpty()){

            try {
                //2.EasyExcel读取---集合
                List<Author> list= EasyExcel.read(file.getInputStream(), Author.class, new ReadListener() {
                    @Override
                    public void invoke(Object o, AnalysisContext analysisContext) {
                    }
                    @Override
                    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                    }
                }).doReadAllSync();

                //3.批量新增
                if(dao.insertBatch(list)>0){
                    return R.ok();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return R.fail();
    }
    @Override
    public void exportData(HttpServletResponse response) {
        //1.设置下载的响应消息头
        response.setHeader("Content-Disposition", "attachment;filename=user_" + System.currentTimeMillis()+".xlsx");
        //2.查询数据
        List<Author> list=dao.selectAll();
        //3.生成Excel 同时写出
        try {
            EasyExcel.write(response.getOutputStream(),Author.class).sheet("自动导出").doWrite(list);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public R queryEcharts() {
        //1.查询数据
        List<AuthorDto> list=dao.selectNum();
        //2.组装数据
        Map<String, ArrayList> map=new HashMap<>();
        map.put("name",new ArrayList());
        map.put("num",new ArrayList());
        list.forEach(d->{
            map.get("name").add(d.getName());
            map.get("num").add(d.getNum());
        });
        return R.ok(map);
    }
}
